package com.code.qgjx.service.impl;

import com.code.qgjx.entity.User;
import com.code.qgjx.mapper.UserMapper;
import com.code.qgjx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        return userMapper.findByUsernameAndPassword(username, password);
    }

    @Override
    public boolean register(User user) {
        // 检查用户名是否已存在
        if (checkUsernameExists(user.getUsername())) {
            return false;
        }
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean updateUser(User user) {
        return userMapper.update(user) > 0;
    }

    @Override
    public boolean deleteUser(Long id) {
        return userMapper.deleteById(id) > 0;
    }

    @Override
    public boolean checkUsernameExists(String username) {
        return userMapper.findByUsername(username) != null;
    }

    @Override
    public List<User> getUserList(String role, String keyword, int offset, int limit) {
        return userMapper.selectList(role, keyword, offset, limit);
    }

    @Override
    public int getUserCount(String role, String keyword) {
        return userMapper.selectCount(role, keyword);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.findById(id);
    }
} 